package com.sinocare.project.common.result;

import lombok.*;

import java.io.Serializable;

@Data
@NoArgsConstructor
@Builder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Result<T> implements Serializable {
    private static final long serialVersionUID = -1084988473496023314L;
    //是否成功
    private Boolean success;
    //返回状态码
    private String code;
    //返回信息
    private String msg;
    //LayUI统计记录数
    private Long count;
    //返回数据
    private T data;
    //响应结果
    private Object result;

    //创建ResultBean对象，接收数据
    protected static <T> Result<T> build(T data) {
        Result<T> result = new Result<T>();
        if (data != null)
            result.setData(data);
        return result;
    }

    /**
     * 操作成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> ok(ResultCode resultCode, T data) {
        Result<T> result = build(data);
        result.setSuccess(true);    //操作成功，默认为true
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMessage());
        return result;
    }

    /**
     * @param resultCode
     * @param count
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> ok(ResultCode resultCode, Long count, T data) {
        Result<T> result = Result.ok(resultCode, data);
        result.setCount(count);
        return result;
    }

    //重载ok
    public static <T> Result<T> ok() {
        return Result.ok(ResultCode.SUCCESS, null);
    }

    /**
     * 操作失败
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> fail(ResultCode resultCode, T data) {
        Result<T> result = build(data);
        result.setSuccess(false);    //操作失败，默认为false
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMessage());
        return result;
    }

    //重载fail
    public static <T> Result<T> fail() {
        return Result.fail(ResultCode.FAIL, null);
    }

    /**
     * 失败
     */
    public static Result error(String code, String message) {
        Result rb = new Result();
        rb.setCode(code);
        rb.setMsg(message);
        rb.setResult(null);
        return rb;
    }

    /**
     * 失败
     */
    public static Result error( String message) {
        Result rb = new Result();
        rb.setCode("-1");
        rb.setMsg(message);
        rb.setResult(null);
        return rb;
    }
}